<?php

/**
 * 
 * @package urCronPlugin
 * @author Marc Weistroff <marc.weistroff@gmail.com>
 * @version $Id: urCron.class.php 19 2009-06-10 15:37:38Z flambeur $
 */
class urCron {
    
    static protected $instance;
    
    public function doCronJob()
    {
      $feeds = Doctrine::getTable('urCronFeed')->findAll();
      foreach($feeds as $feed)
      {
        $class = $feed->getClass();
        $f = new $class();
        $f->setFeedUri($feed->getFeedUri());
        $f->setFeedId($feed->getId());
         
        if(time() - strtotime($feed->getLastCheckedAt()) >= sfConfig::get('app_urcron_update_frequency', 1500))
        {
          try
          {
            $f->doCronJob();
            $feed->setLastCheckedAt(date('Y-m-d H:i:s', time()));
            $feed->save();
          }
          catch(Exception $e)
          {
            $l = sfContext::getInstance()->getLogger();
            $l->log($e->getMessage(), 'error');
          }
        }
      }
    }

    /**
     * Get the class of the currently used theme
     * @return object Theme Class (urCronThemeDefaultPlugin)
     */
    public function getTheme()
    {
      $themeClass = "urCronTheme" . sfConfig::get('app_urcron_theme', 'Default');
      return new $themeClass;
    }
 
    static function getInstance()
    {
      if(!self::$instance instanceof urCron)
      {
        self::createInstance();
      }
      return self::$instance;
    }

    static function createInstance()
    {
      self::$instance = new urCron();
    }
}
?>
